package com.newsvoice.activity;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import com.newsvoice.utils.*;
import com.newsvoice.activity.R;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.Spanned;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class RssListAdapter extends ArrayAdapter<JSONObject> {
	private String url;
    private ImageLoader imageLoader;

	public RssListAdapter(Activity activity, List<JSONObject> imageAndTexts) {
		super(activity, 0, imageAndTexts);
        imageLoader=new ImageLoader(activity.getApplicationContext());
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Activity activity = (Activity) getContext();
		LayoutInflater inflater = activity.getLayoutInflater();

		// Inflate the views from XML
		View rowView = inflater.inflate(R.layout.image_text_layout, null);
		JSONObject jsonImageText = getItem(position);

		// ////////////////////////////////////////////////////////////////////////////////////////////////////
		// The next section we update at runtime the text - as provided by the
		// JSON from our REST call
		// //////////////////////////////////////////////////////////////////////////////////////////////////
		TextView textView = (TextView) rowView.findViewById(R.id.job_text);
		final ImageView imageView = (ImageView) rowView
				.findViewById(R.id.feed_image);

		try {

			if (jsonImageText.get("imageLink") != null) {
				url = (String) jsonImageText.get("imageLink");
				if (url.length() == 0 || url.contains("vietnamnet")) {
					imageView.setImageResource(R.drawable.image_news_default);
				} 
				else {
					if (url.contains(".jpg?w")) {
						url = url.split("\\?")[0];
					}
			        imageLoader.DisplayImage(url, imageView);

//					new Thread(new Runnable() {
//
//						@Override
//						public void run() {
//							// TODO Auto-generated method stub
//							InputStream in = getInputStreamFromUrl(url);
//							if (in != null) {
//								final Bitmap bitmap = BitmapFactory
//										.decodeStream(in);
//								imageView.post(new Runnable() {
//
//									@Override
//									public void run() {
//										// TODO Auto-generated method stub
//										imageView.setImageBitmap(bitmap);
//									}
//								});
//							}
//						}
//					}).start();
				}
				// URL feedImage= new URL(url);
				//
				// HttpURLConnection conn=
				// (HttpURLConnection)feedImage.openConnection();
				// InputStream is = conn.getInputStream();
				// LoadImage loadImage = new LoadImage(url, imageView);
				// loadImage.execute();

			}
			Spanned text = (Spanned) jsonImageText.get("text");
			textView.setText(text);

		} catch (JSONException e) {
			textView.setText("JSON Exception");
		}

		return rowView;

	}

	private InputStream getInputStreamFromUrl(String url) {
		Log.d("load image", url);
		InputStream content = null;

		HttpGet httpGet = new HttpGet(url);
		HttpClient httpclient = new DefaultHttpClient();
		// Execute HTTP Get Request
		HttpResponse response = null;
		try {
			response = httpclient.execute(httpGet);
			content = response.getEntity().getContent();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			Log.d("load image ClientProtocolException", e.getMessage());
		} 
		return content;
	}

}